using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Web.Helper;
using Ent.Voice;
using Ent.Voice.Show;
using Ent.Voice.Nomination;
using System.Collections.Generic;
using Ent.Web.WebControl;
public partial class Search : System.Web.UI.Page
{
    int catId;
    int eventId;
    int entId;
    int count = 5;
    string key;
    DataTable retValue = new DataTable();
    public Search()
    {
        this.PreRender += new EventHandler(Search_PreRender);
    }

    void Search_PreRender(object sender, EventArgs e)
    {
        cSearch.CatagoryId = catId;
        cSearch.EntityId = entId;
        cSearch.key = key;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateList();
    }
    private void PopulateList()
    {
        try
        {
            retValue.Columns.Add("Type");
            retValue.Columns.Add("Id");
            retValue.Columns.Add("Name");
            retValue.Columns.Add("Param1");
            retValue.Columns.Add("Param2");
            retValue.Columns.Add("Param3");
            retValue.Columns.Add("Param4");
            retValue.Columns.Add("Param5");
            retValue.Columns.Add("Param6");
            retValue.Columns.Add("Param7");
            retValue.Columns.Add("Param8");
            retValue.Columns.Add("Param9");
            retValue.Columns.Add("Param10");
            retValue.Columns.Add("Param11");
            catId = MathFunction.ConvertToInteger(Request.QueryString["cat"], -1);
            entId = MathFunction.ConvertToInteger(Request.QueryString["ent"], 1);
            eventId = MathFunction.ConvertToInteger(Request.QueryString["ev"], -1);
            int pi = MathFunction.ConvertToInteger(Request.QueryString["pi"], 0);
            pi = pi == 0 ? 1 : pi;
            key = Request.QueryString["key"];
            int start = (pi - 1) * count + 1;

            int total = 1;
            DataTable dt = DoSearch(ref total, start);
            int pageCount = dt.Rows.Count;
            lblHeader.Text = ResourceHelper.Get("MSG_SearchHeader") + " " + start.ToString() + " to " + Convert.ToInt32(start + pageCount - 1) + " of " + total.ToString() + " records.";
            SetNavigation(pi, total, start);
            if (dt != null && dt.Rows.Count != 0)
            {
                dgSearchList.DataSource = dt;
                dgSearchList.DataBind();
            }
            else
            {
                lblHeader.Text = ResourceHelper.Get("MSG_NoResult");
                dgSearchList.Visible = false;
            }
        }
        catch (Exception exc)
        {
            Common.ErrorPage(exc);
        }
    }
    private void SetNavigation(int pi, int total, int start)
    {
        Dictionary<string, string> kvList = new Dictionary<string, string>();
        kvList.Add("pi", "");
        btnPrev.Visible = false;
        btnNext.Visible = false;
        if (pi > 1)
        {
            btnPrev.Visible = true;
            kvList["pi"] = Convert.ToString(pi - 1);
            btnPrev.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
        if (total >= start + count)
        {
            btnNext.Visible = true;
            kvList["pi"] = Convert.ToString(pi + 1);
            btnNext.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
    }
    private DataTable DoSearch(ref int total, int startIndex)
    {
        switch (entId)
        { 
            case 1:
                Event[] evs = Event.Search(catId, key, startIndex, count, "Name", ref total);
                if (evs == null)
                    break;
                for (int i = 0; i < evs.Length; i++)
                {
                    Event ev = (Event)evs.GetValue(i);
                    DataRow dr = SetEventDataRow(ev);
                    if (dr != null)
                        retValue.Rows.Add(dr);
                }
                break;
            case 2:
                Ent.Voice.User[] us = Ent.Voice.User.Search(key, startIndex, count, "Fname", ref total);
                if (us == null)
                    break;
                for (int i = 0; i < us.Length; i++)
                {
                    User u = (User)us.GetValue(i);
                    DataRow dr = SetUserDataRow(u);
                    if (dr != null)
                        retValue.Rows.Add(dr);
                }
                break;
            case 3:
                Entry[] ens = Entry.Search(EntryFilter.Event, eventId.ToString(), key, startIndex, count, "Title", false, StringHelper.ConvertToString(Session["USERID"]), ref total);
                if (ens == null)
                    break;
                for (int i = 0; i < ens.Length; i++)
                {
                    Entry en = (Entry)ens.GetValue(i);
                    DataRow dr = SetEntryDataRow(en); 
                    if (dr != null)
                        retValue.Rows.Add(dr);
                }
                break;
            case -1:
            default:
                object[] objs = Global.Search(catId, eventId, key, startIndex, count, "Name", "Title", "Fname", false, StringHelper.ConvertToString(Session["USERID"]), ref total);
                if (objs == null)
                    break;
                for (int i = 0; i < objs.Length; i++)
                {
                    object obj = objs.GetValue(i);
                    if (obj == null)
                        continue;
                    object[] obs = (object[])obj;
                    for (int j = 0; j < obs.Length; j++)
                    {
                        object ob = obs.GetValue(j);
                        if (ob == null)
                            continue;
                        DataRow dr = null;
                        switch (ob.GetType().Name)
                        {
                            case "Event":
                                dr = SetEventDataRow((Event)ob);
                                break;
                            case "User":
                                dr = SetUserDataRow((User)ob);
                                break;
                            case "Entry":
                                dr = SetEntryDataRow((Entry)ob);
                                break;
                        }
                        if (dr != null)
                            retValue.Rows.Add(dr);
                    }
                }
                break;
        }
        return retValue;
    }
    private DataRow SetEventDataRow(Event ev)
    {
        if (ev != null)
        {
            DataRow dr = retValue.NewRow();
            dr["Type"] = 1;
            dr["Id"] = ev.Id;
            dr["Name"] = ev.Name;
            dr["Param2"] = ev.Organiser.FName + " " + ev.Organiser.LName;
            dr["Param1"] = ev.Organiser.UserID;
            dr["Param3"] = ev.Status.ToString();
            dr["Param4"] = ev.CatagoryId;
            dr["Param5"] = ev.Count;
            dr["Param6"] = ev.Votes;
            return dr;
        }
        else return null;
    }
    private DataRow SetUserDataRow(User u)
    {
        if (u != null)
        {
            DataRow dr = retValue.NewRow();
            dr["Type"] = 2;
            dr["Id"] = u.UserID;
            dr["Name"] = u.FName + " " + u.LName;
            dr["Param1"] = u.Location;
            dr["Param2"] = u.Hobbies;
            dr["Param3"] = u.CntBronze;
            dr["Param4"] = u.CntSilver;
            dr["Param5"] = u.CntGold;
            dr["Param6"] = u.cntParticipate;
            dr["Param7"] = u.Points;
            dr["Param8"] = u.PhotoId;
            
            return dr;
        }
        else return null;
    }
    private DataRow SetEntryDataRow(Entry en)
    {
        if (en != null)
        {
            DataRow dr = retValue.NewRow();
            dr["Type"] = 3;
            dr["Id"] = en.Id;
            dr["Name"] = en.Title;
            if (en.Nominee != null)
            {
                dr["Param2"] = en.Nominee.FName + " " + en.Nominee.LName;
                dr["Param1"] = en.Nominee.UserID;
            }
            dr["Param3"] = en.Status;
            dr["Param4"] = en.OrganiserId;
            dr["Param5"] = en.Views;
            dr["Param6"] = en.Votes;
            dr["Param7"] = en.Points;
            dr["Param8"] = en.Upload.Location;
            dr["Param9"] = en.Upload.Type;
            dr["Param10"] = en.Description;
            dr["Param11"] = en.Upload.Text;
            return dr;
        }
        else return null;
    }
    protected void dgSearchList_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex >= 0)
        {
            string str = "";
            switch(MathFunction.ConvertToInteger(e.Item.Cells[0].Text))
            {
                case 1:
                    str = "<table cellpadding='0' cellspacing='0' class='general'>";
                    str += "<tr>";
                    str += "<td class='generaltd' style='width: 30px'><img src='Images/dj/event.gif' /></td>";
                    str += "<td class='generaltd' style='width: 200px'><a href=ShowEvent.aspx?id=" + e.Item.Cells[1].Text + ">" + StringHelper.RestrictMaxChar(e.Item.Cells[2].Text, 20) + "</a><br />";
                    str += ResourceHelper.Get("LBL_OrganisedBy") + "&nbsp;<a href=ShowUser.aspx?id=" + e.Item.Cells[3].Text + ">" + StringHelper.RestrictMaxChar(e.Item.Cells[4].Text, 20) + "</a></td>";
                    str += "<td class='generaltd'>" + ResourceHelper.Get("LBL_Catagory") + "&nbsp;" + Common.GetSettingValue(Collection.Catagory, Convert.ToInt32(e.Item.Cells[6].Text)) + "<br />";
                    str += ResourceHelper.Get("LBL_Status") + "&nbsp;" + e.Item.Cells[5].Text + "<br />";
                    str += ResourceHelper.Get("LBL_Votes") + "&nbsp;" + e.Item.Cells[8].Text + "<br />";
                    str += ResourceHelper.Get("LBL_Entries") + "&nbsp;" + e.Item.Cells[7].Text + "</td>";
                    str += "</tr>";
                    str += "</table>";
                    e.Item.Cells[0].Text = str;
                    break;
                case 2:
                    str = "<table cellpadding='0' cellspacing='0' class='general'>";
                    str += "<tr>";
                    str += "<td class='generaltd' style='width: 30px'><img src='Images/dj/user.gif' /></td>";
                    str += "<td class='generaltd' style='width: 200px'><a href=ShowUser.aspx?id=" + e.Item.Cells[1].Text + ">" + StringHelper.RestrictMaxChar(e.Item.Cells[2].Text, 20) + "</a><br />";
                    str += "<a href=ShowUser.aspx?id=" + e.Item.Cells[1].Text + "><img src='" + Common.GetUserImage(e.Item.Cells[10].Text) + "' style='height: 50px' /></a></td>";
                    int CntGold = MathFunction.ConvertToInteger(e.Item.Cells[7].Text);
                    int CntSilver = MathFunction.ConvertToInteger(e.Item.Cells[6].Text);
                    int CntBronze = MathFunction.ConvertToInteger(e.Item.Cells[5].Text);
                    str += "<td class='generaltd'>";
                    if (!(CntBronze <= 0 && CntGold <= 0 && CntSilver <= 0))
                    {
                        str += ResourceHelper.Get("LBL_Medal");
                        if (CntGold != 0)
                            str += "<img src=\"Images/dj/trophy1.gif\" style='height:20px' />&nbsp;:&nbsp;" + CntGold + "&nbsp;&nbsp;&nbsp;&nbsp;";
                        if (CntSilver != 0)
                            str += "<img src=\"Images/dj/trophy2.gif\" style='height:20px' />&nbsp;:&nbsp;" + CntSilver + "&nbsp;&nbsp;&nbsp;&nbsp;";
                        if (CntBronze != 0)
                            str += "<img src=\"Images/dj/mon.gif\" style='height:20px' />&nbsp;:&nbsp;" + CntBronze + "<br />";
                    }
                    str += ResourceHelper.Get("LBL_Part") + "&nbsp;" + e.Item.Cells[8].Text + "<br />";
                    str += ResourceHelper.Get("LBL_Points") + "&nbsp;" + e.Item.Cells[9].Text + "<br />";
                    str += ResourceHelper.Get("LBL_Loc") + "&nbsp;" + e.Item.Cells[3].Text + "</td>";
                    str += "</tr>";
                    str += "</table>";
                    e.Item.Cells[0].Text = str;
                    break;
                case 3:
                    Entry en = new Entry();
                    en.Id = MathFunction.ConvertToInteger(e.Item.Cells[1].Text);
                    en.Title = e.Item.Cells[2].Text;
                    en.Description = e.Item.Cells[12].Text;
                    en.Upload = new File();
                    en.Upload.Location = e.Item.Cells[10].Text;
                    en.Upload.Type = FileHelper.GetFileType(e.Item.Cells[11].Text);
                    en.Upload.Text = e.Item.Cells[13].Text;
                    en.Status = (e.Item.Cells[5].Text != "" ? (EntryStatus)Enum.Parse(typeof(EntryStatus), e.Item.Cells[5].Text, true) : EntryStatus.Live);
                    en.Views = MathFunction.ConvertToInteger(e.Item.Cells[7].Text);
                    en.Votes = MathFunction.ConvertToInteger(e.Item.Cells[8].Text);
                    en.Points = MathFunction.ConvertToInteger(e.Item.Cells[9].Text);
                    en.Nominee = new User();
                    en.Nominee.FName = e.Item.Cells[4].Text;
                    en.Nominee.UserID = e.Item.Cells[3].Text;
                    en.OrganiserId = e.Item.Cells[6].Text;
                    Page p = (Page)this;
                    DashEntry de = (DashEntry)Common.CreateDashEntry(ref p, en, EntryMode.Search);
                    e.Item.Cells[0].Controls.Add(de);
                    break;
                default:
                    break;
            }
        }
        e.Item.Cells[1].Visible = false;
        e.Item.Cells[2].Visible = false;
        e.Item.Cells[3].Visible = false;
        e.Item.Cells[4].Visible = false;
        e.Item.Cells[5].Visible = false;
        e.Item.Cells[6].Visible = false;
        e.Item.Cells[7].Visible = false;
        e.Item.Cells[8].Visible = false;
        e.Item.Cells[9].Visible = false;
        e.Item.Cells[10].Visible = false;
        e.Item.Cells[11].Visible = false;
        e.Item.Cells[12].Visible = false;
        e.Item.Cells[13].Visible = false;
    }
}
